<?php
namespace app\action\admin;
use core\BaseAction;
use core\App;
use core\JsonInfo;
use core\ParamFilter;
class DicAction extends BaseAction{

	function getList($params,$req,$res){
		return $this->view("admin.dic_list");
	}
	function postList($params){
		$kindId = $params['id'];
		if(empty($kindId)){
			//初始化查询过滤器
			$filter = new ParamFilter($params);
			$filter->addFilter('Q_key_notnull',true);
			$filter->setOrder('id','desc');
			//调用业务逻辑
			$service = App::bean('\\app\\service\\admin\\DicService');
			$service->queryByFilter($filter);
			$rows = $filter->grid->rows;
			foreach($rows as $i=>$r){
				$r->state = 'closed';
			}
			$this->json($filter->grid);
		}else{
			$service = App::bean('\\app\\service\\admin\\DicService');
			$list = $service->findDicItemsByKindId($kindId);
			foreach($list as $i=>$r){
				$r->state = 'open';
			}
			$this->json($list);
		}
	}
	
	function postAddKind($params){
		$info = new JsonInfo();
		if(empty($params['name'])||empty($params['key'])){
			$info->msg = "非法数据";
		}else{
			$service = App::bean('\\app\\service\\admin\\DicService');
			$service->saveDic($params,$info);
		}
		return $this->json($info);
	}
	
	function postAddItem($params){
		$info = new JsonInfo();
		if($params['name']==''||$params['kind_id']==''||$params['value']==''){
			$info->msg = "非法数据";
		}else{
			if($params['status']) $params['status']=1;
			else $params['status']=0;
			$service = App::bean('\\app\\service\\admin\\DicService');
			$service->saveDic($params,$info);
		}
		return $this->json($info);
	}
	
	function postDelete($params){
		$info = new JsonInfo();
		if(empty($params['ids'])){
			$info->msg = "非法数据";
		}else{
			$idArr = explode(',',$params['ids']);
			$service = App::bean('\\app\\service\\admin\\DicService');
			$table = $service->table()
				->whereIn('id',$idArr)
				->orWhereIn('kind_id',$idArr);
			$filter = new ParamFilter($params);
			$info = $service->deleteByFilter($filter,$table);
		}
		return $this->json($info);
	}
}